package com.lhd.mpaas.demo;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import com.alipay.mobile.common.logging.api.LoggerFactory;
import com.alipay.mobile.framework.quinoxless.QuinoxlessApplicationLike;
import com.alipay.mobile.nebula.appcenter.model.AppInfo;
import com.alipay.mobile.nebula.provider.H5AppCenterPresetProvider;
import com.alipay.mobile.nebula.provider.H5AppProvider;
import com.alipay.mobile.nebula.util.H5Utils;
import com.apkfuns.logutils.LogUtils;

public class MyApplication extends QuinoxlessApplicationLike implements Application.ActivityLifecycleCallbacks {

    public static final String TAG = MyApplication.class.getSimpleName();

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        LogUtils.d("======>>>attacheBaseContext");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
        LogUtils.d("======>>>onCreate");
    }

    @Override
    public void onMPaaSFrameworkInitFinished() {
        LogUtils.d("======>>>onMPaaSFrameworkInitFinished");
        LoggerFactory.getTraceLogger().info(TAG, getProcessName());

        // ======>>> 小程序前置条件
        // 实现 H5AppCenterPresetProvider 接口类，并在启动时设置该 Provider 实例
        H5Utils.setProvider(H5AppCenterPresetProvider.class.getName(), new H5AppCenterPresetProviderImpl());
//        // 离线资源包
        PresetAmrPipeline presetAmrPipeline = new PresetAmrPipeline();
        presetAmrPipeline.run();

        H5AppProvider provider = H5Utils.getProvider(H5AppProvider.class.getName());
        // 获取应用配置
        AppInfo appInfo = provider.getAppInfo("2019111412345678");
        // 某版本应用是否已经安装
        boolean isInstalled = provider.isInstalled("2019111412345678", "1.0.2.0");
        // 某版本应用离线包是否已经下载完成
        boolean isAvailable = provider.isAvailable("2019111412345678", "1.0.2.0");

        LogUtils.d(appInfo);
        LogUtils.d(isInstalled);
        LogUtils.d(isAvailable);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

}
